package it.polimi.traveldream;

import it.polimi.traveldream.entities.Excursion;
import it.polimi.traveldream.entities.Hotel;
import it.polimi.traveldream.entities.PackageBase;
import it.polimi.traveldream.entities.Transport;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 * Session Bean implementation class GestioneShowPacchetto
 */
@Stateless
public class GestioneShowPacchetto implements GestioneShowPacchettoLocal {

	@PersistenceContext
    private EntityManager em;
	
    /**
     * Default constructor. 
     */
    public GestioneShowPacchetto() {
        // TODO Auto-generated constructor stub
    }
    
    @Override
    public List<PackageDTO> show(){
    	/*Query q = em.createNamedQuery("Package.findAll");
    	return convert(q.getResultList());*/
    	
    	String query="SELECT s FROM PackageBase s ";
		    	
    	Query q=em.createQuery(query,PackageBase.class);
    	return convert(q.getResultList());
    }
    
    public ArrayList<HotelDTO> hotelToHotelDTO(List<Hotel> hotels) {
		
		ArrayList<HotelDTO>tmp=new ArrayList<HotelDTO>();
		for(Hotel in :hotels){
			
			HotelDTO pk=new HotelDTO();
			pk.setId(in.getId());
			pk.setName(in.getName());;
			pk.setIndirizzo(in.getIndirizzo());
			pk.setDescription(in.getDescription());
			pk.setPrice(in.getPrice());
			pk.setStelle(in.getStelle());
			
			tmp.add(pk);
	      }
		
		return tmp;
	}
    
    public ArrayList<ExcursionDTO> excursionToExcursionDTO(List<Excursion> excursions) {
		
		ArrayList<ExcursionDTO>tmp=new ArrayList<ExcursionDTO>();
		for(Excursion in :excursions){
			
			ExcursionDTO pk=new ExcursionDTO();
			pk.setId(in.getId());
			pk.setName(in.getName());;
		    pk.setDurata(in.getDurata());
		    pk.setDescription(in.getDescription());
		    pk.setPrice(in.getPrice());
			
			tmp.add(pk);
	      }
		
		return tmp;
	}
    
    public ArrayList<TransportDTO> transportToTransportDTO(List<Transport> voli){
		ArrayList<TransportDTO>tmp=new ArrayList<TransportDTO>();
		for(Transport in :voli){
			
			TransportDTO pk=new TransportDTO();
			pk.setId(in.getId());
			pk.setNome(in.getNome());
			pk.setPrezzo(in.getPrezzo());
			pk.setDataPartenza(in.getDataPartenza());
			pk.setFrequenza(in.getFrequenza());
			pk.setNPosti(in.getNPosti());
			pk.setCittaPartenza(in.getCittaPartenza());
			pk.setCittaArrivo(in.getCittaArrivo());
			
			tmp.add(pk);
	      }
		
		return tmp;
		
	}


    
    
    
    
    //metodo richiamato all'interno della show che ricevuta la lista di pacchetti li converte in dto
    public List<PackageDTO> convert(List<PackageBase> list1){
    	System.out.println(list1.get(0).getNome());
    	
    	List<PackageDTO>tmp=new ArrayList<PackageDTO>();
    	
    	System.out.println("Sto convertendo i pacchetti");
    	
    	for(PackageBase h: list1){
    		
    		PackageDTO htmp=new PackageDTO();
    		htmp.setId(h.getId());
    		htmp.setNome(h.getNome());
    		htmp.setCitta(h.getCitta());
    		htmp.setPrezzo(h.getPrezzo());
    		htmp.setDescrizione(h.getDescrizione());
    		htmp.setDurataviaggio(h.getDurataviaggio());
    		htmp.setDisponibile(h.getDisponibile());
    		htmp.setInOfferta(h.getInOfferta());
    		System.out.println("PRIMA DI CONVERSIONE ESCURSIONI");
    		htmp.setEscursioni(this.excursionToExcursionDTO(h.getExcursions()));
    		System.out.println("CONVERSIONE ESCURSIONI");
    		htmp.setHotel(this.hotelToHotelDTO(h.getHotels()));
    		System.out.println("CONVERSIONE HOTEL");
    		htmp.setVoliandata(this.transportToTransportDTO(h.getTransports()));
    		System.out.println("CONVERSIONE ANDATA");
    		htmp.setVoliritorno(this.transportToTransportDTO(h.getTransportsritorno()));
    		System.out.println("CONVERSIONE RITORNO");
    		
    		tmp.add(htmp);
    	}
    	
    	return tmp;
    	
    }

}
